#include "StdAfx.h"
#include "LPWorkShop.h"
#include "LPModel.h"

CLPWorkShop::CLPWorkShop(void)
	: m_pLPModel(NULL)
	, m_pConstraintParam(NULL)
	, m_iDecVarCount(0)
{
	m_pLPModel = new CLPModel;
}

CLPWorkShop::~CLPWorkShop(void)
{
	if (m_pLPModel != NULL)
	{
		delete m_pLPModel;
		m_pLPModel = NULL;
	}
}

bool CLPWorkShop::SetMaterial(CConstraintParam* pConstraintParam, std::vector<std::vector<CLPModelDecVarFactor>>& vDecVarArr)
{
	if (NULL == m_pLPModel || NULL == pConstraintParam || vDecVarArr.empty())
		return false;

	m_pConstraintParam = pConstraintParam;
	m_vDecVarArr = vDecVarArr;
	m_iDecVarCount = 0;

	for (int i=0; i<(int)m_vDecVarArr.size(); ++i)
	{
		m_iDecVarCount += (int)m_vDecVarArr.at(i).size();
	}

	return true;
}

bool CLPWorkShop::Produce()
{
	if (NULL == m_pLPModel || NULL == m_pConstraintParam || m_vDecVarArr.empty())
		return false;
	
	m_pLPModel->SetDecVar(m_vDecVarArr);

	m_pLPModel->SetConstraint(m_pConstraintParam);

	m_pLPModel->SetObjFun();

	m_pLPModel->MORSolve();

	return true;
}

bool CLPWorkShop::GetProduct(std::vector<std::vector<CLPModelDecVarFactor>>& vQualifiedDecVarArr,
							 std::vector<std::vector<CLPModelDecVarFactor>>& vUnQualifiedDecVarArr)
{
	if (NULL == m_pLPModel || NULL == m_pConstraintParam || m_vDecVarArr.empty())
		return false;

	if (m_iDecVarCount != m_pLPModel->GetDecVarCount())
		return false;

	int* pSolution = new int[m_iDecVarCount];
	m_pLPModel->GetSolution(pSolution);

	vQualifiedDecVarArr.clear();
	vUnQualifiedDecVarArr.clear();

	int iStatCount = 0;
	for (int i=0; i<(int)m_vDecVarArr.size(); ++i)
	{
		std::vector<CLPModelDecVarFactor> vBlastPileDecVar = m_vDecVarArr.at(i);
		std::vector<CLPModelDecVarFactor> vQualifiedDecVar;
		std::vector<CLPModelDecVarFactor> vUnQualifiedDecVar;

		for (int j=0; j<(int)vBlastPileDecVar.size(); ++j)
		{
			CLPModelDecVarFactor decVar = vBlastPileDecVar.at(j);
		
			if (1 == pSolution[iStatCount])
			{
				decVar.m_iSolutionValue = 1;
				vQualifiedDecVar.push_back(decVar);
			}
			else 
			{
				decVar.m_iSolutionValue = 0;
				vUnQualifiedDecVar.push_back(decVar);
			}
			iStatCount++;
		}///:~for--j
		vQualifiedDecVarArr.push_back(vQualifiedDecVar);
		vUnQualifiedDecVarArr.push_back(vUnQualifiedDecVar);
	}///:~for--i

	if (pSolution != NULL)
	{
		delete [] pSolution;
		pSolution = NULL;
	}

	return true;
}